package com.example.springmvcdemo1.controller;

import com.example.springmvcdemo1.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;


@RequestMapping("/user")   // 类上面的是 RequestMapping 是一级目录 可以省略
@ResponseBody  // 作用：表示返回的是非静态页面的数据
@Slf4j
@Controller
public class UserController {

    // 获取前端的json数据
    @RequestMapping("/login2")
    public HashMap<String,Object> login2(@RequestBody UserInfo userInfo){
        HashMap<String,Object> result = new HashMap<>();
        int state = 200;
        int data = -1;  // data等于1 登录成功，否则失败
        String msg = "未知错误";
        if(StringUtils.hasLength(userInfo.getName()) && StringUtils.hasLength(userInfo.getPassword())) {
            if(userInfo.getName().equals("admin") && userInfo.getPassword().equals("admin")){
                data = 1;
                msg = "";
            }else{
                msg = "用户名或者密码失败";
            }
        }else{  // 参数为空
            msg = "非法参数";
        }
        result.put("state",state);
        result.put("data",data);
        result.put("msg",msg);
        return result;
    }

    @RequestMapping("/login3")
    public HashMap<String, Object> login3(@RequestBody UserInfo userInfo) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        int state = 200;
        int data = -1; // 等于 1，登录成功，否则登录失败
        String msg = "未知错误";
        if (StringUtils.hasLength(userInfo.getName()) && StringUtils.hasLength(userInfo.getPassword())) {
            if (userInfo.getName().equals("admin") && userInfo.getPassword().equals("admin")) {
                data = 1;
                msg = "";
            } else {
                msg = "用户名或密码失败！";
            }
        } else { // 参数为空
            msg = "非法参数";
        }
        result.put("state", state);
        result.put("data", data);
        result.put("msg", msg);
        return result;
    }



    // 从配置文件中读取图片的保存路径
    @Value("${img.path}")
    private String imgPath;

    @RequestMapping("/sayhi")
    public String sayHi(){
        return "东厂，韩正淳";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/sayhi2")  // 404
    public String sayHi2(){
        return "西厂，韩高辉";
    }


    @PostMapping("/sayhi3")
    public String sayHi3(){
        return "姜帅";
    }

    @GetMapping("/sayhi4")
    public String sayHi4(){
        return "shuaibi";
    }

    @RequestMapping("/login")
    public String login(@RequestParam(value = "name",required = false) String username, String password){
        return "用户名：" + username + "密码：" + password;
    }

    @RequestMapping("/reg")
    public String reg(UserInfo userInfo){
        return "用户信息" + userInfo;
    }

    @RequestMapping("/hero/{id}/{name}")
    public String getHeroInfo(@PathVariable Integer id, @PathVariable String name){
        return "ID:" + id + "name:" + name;
    }

    @RequestMapping("/upimg")
    public boolean upImg(Integer uid, @RequestPart("img") MultipartFile file){  //img--前端传递的  MulipartFile--专门接收文件
        boolean result = false;
        String fileName = file.getOriginalFilename(); // 得到原图片的名称
        fileName = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID().toString() + fileName;
        //保存图片到本地目录
        try {
            file.transferTo(new File(imgPath +fileName));
            result = true;
        } catch (IOException e) {
            log.error("上传图片失败" + e.getMessage());
        }
        return  result;
    }
}
